package sky.wicket;

import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxFallbackLink;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.spring.injection.annot.SpringBean;
import sky.domain.Flight;
import sky.service.FlightService;
import sky.tro.TicketFinder;

import java.text.SimpleDateFormat;

/**
 * Created with IntelliJ IDEA.
 * User: Gauss
 * Date: 29.08.13
 * Time: 13:52
 * To change this template use File | Settings | File Templates.
 */
public class BookingFlight extends BasePage {

    @SpringBean
    private FlightService flightService;

    public BookingFlight() {
        super();
    }

    public BookingFlight(final TicketFinder ticketFinder) {
        super();
        ListView<Flight> listView = new ListView<Flight>("flights", flightService.FindByFinder(ticketFinder, "depDate", ticketFinder.getTicketType())) {
            @Override
            protected void populateItem(ListItem<Flight> components) {
                final Flight flight = components.getModelObject();
                components.add(new Label("id", flight.getId().toString()));
                components.add(new Label("depDate", new SimpleDateFormat("dd.MM.yyyy HH:mm").format(flight.getDepartureDate())));
                components.add(new Label("depAir", flight.getDepartureAirport().getName()));
                components.add(new Label("desAir", flight.getDestinationAirport().getName()));
                components.add(new Label("cost", flight.getEconomCost().toString()));
                components.add(new Link("chooseLink") {
                    @Override
                    public void onClick() {
                        setResponsePage(new Personals(ticketFinder, flight.getId()));
                    }
                });
                //container with flight information
                final WebMarkupContainer container = new WebMarkupContainer("flightInfoContent");
                container.setOutputMarkupPlaceholderTag(true);
                container.setVisible(false);
                container.add(new Label("EDepDate", new SimpleDateFormat("dd.MM.yyyy HH:mm").format(flight.getDepartureDate())));
                container.add(new Label("EDesDate", new SimpleDateFormat("dd.MM.yyyy HH:mm").format(flight.getDestinationDate())));
                //BookmarkableLinksAndLabels
                container.add(new BookmarkablePageLink("EDepAir", HomePage.class)
                        .add(new Label("depAirLabel", flight.getDepartureAirport().getName()))
                );
                container.add(new BookmarkablePageLink("EDesAir", HomePage.class)
                        .add(new Label("desAirLabel", flight.getDestinationAirport().getName()))
                );
                container.add(new BookmarkablePageLink("EDepCity", HomePage.class)
                        .add(new Label("depCityLabel", flight.getDepartureAirport().getCity().getName()))
                );
                container.add(new BookmarkablePageLink("EDesCity", HomePage.class)
                        .add(new Label("desCityLabel", flight.getDestinationAirport().getCity().getName()))
                );
                container.add(new BookmarkablePageLink("EDepCountry", HomePage.class)
                        .add(new Label("depCountryLabel", flight.getDepartureAirport().getCity().getCountry().getName()))
                );
                container.add(new BookmarkablePageLink("EDesCountry", HomePage.class)
                        .add(new Label("desCountryLabel", flight.getDestinationAirport().getCity().getCountry().getName()))
                );
                container.add(new BookmarkablePageLink("EPlane", HomePage.class)
                        .add(new Label("planeLabel", flight.getPlane().getName()))
                );

                container.add(new Label("EFreePlaces", flight.getFreeAllPlaces().toString()));
                container.add(new Label("ECost", flight.getEconomCost().toString()));
                components.add(container);
                //maybe use ordinary js ?
                components.add(new AjaxFallbackLink("expandLink") {
                    @Override
                    public void onClick(AjaxRequestTarget ajaxRequestTarget) {
                        container.setVisible(!container.isVisible());
                        if (ajaxRequestTarget != null) {
                            ajaxRequestTarget.add(container);
                        }
                    }
                });
            }
        };
        add(listView);
    }
}
